<?php
namespace app\admin\controller;

use app\admin\BaseController;
use think\facade\Cache;
use think\facade\Validate;
use think\facade\Db;

use app\admin\model\Admin;
use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use app\admin\model\AdminRoleMenu;

/* 
 * 菜单管理
 */
class MenuController extends BaseController
{
    /*
     * 菜单列表
     * author xfwed
     * date 2022.03.24 14:31 
     * ------------------------------------------
     * update phper  2022-03-24 07:10 将token记入缓存
    */
    public function menuList()
    {
        $list = AdminMenu::getMenu();
        return $this->ajaxReturn(100, '查询成功', $list);
    }
    
    /*
     * 添加菜单
     * author xfwed
     * date 2022.03.24 14:49
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function Addmenu()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'parent_id|父级id' => 'require',
            'menu_name|栏目名称' => 'require',
            'module|模块名' => 'require',
            'controller|控制器名' => 'require',
            'action|方法名' => 'require',
            'sort|排序值' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        if($all['parent_id'] == 0 ){
            $level = 1;
        }  else {
            $l = AdminMenu::where('id',$all['parent_id'])->value('level');
            $level = $l + 1;
        }
        $row = AdminMenu::where('module',$all['module'])
                ->where('controller',$all['controller'])
                ->where('action',$all['action'])
                ->where('status',1)
                ->find();
        if($row){
            return $this->ajaxReturn(3, '不要重复添加', 'default');
        }
        
        $data['parent_id'] = $all['parent_id'];
        $data['menu_name'] = $all['menu_name'];
        $data['module'] = $all['module'];
        $data['controller'] = $all['controller']; 
        $data['action'] = $all['action'];
        $data['icon'] = input('param.icon');
        $data['level'] = $level;
        $data['sort'] = input('param.sort');
           
        
        $add = AdminMenu::insert($data);
        
        return $this->ajaxReturn(100, '添加成功', 'default');
    }
    
    /*
     * 编辑菜单
     * author xfwed
     * date 2022.03.24 15:13
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function saveMenu()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'id|id' => 'require',
            'parent_id|父级id' => 'require',
            'menu_name|栏目名称' => 'require',
            'module|模块名' => 'require',
            'controller|控制器名' => 'require',
            'action|方法名' => 'require',
            'sort|排序值' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        if($all['parent_id'] == 0 ){
            $level = 1;
        }  else {
            $l = AdminMenu::where('id',$all['parent_id'])->value('level');
            $level = $l + 1;
        }
        $row = AdminMenu::where('module',$all['module'])
                ->where('controller',$all['controller'])
                ->where('action',$all['action'])
                ->where('status',1)
                ->where('id','<>',$all['id'])
                ->find();
        if($row){
            return $this->ajaxReturn(3, '不要重复添加', 'default');
        }
        
        $data['parent_id'] = $all['parent_id'];
        $data['menu_name'] = $all['menu_name'];
        $data['module'] = $all['module'];
        $data['controller'] = $all['controller']; 
        $data['action'] = $all['action'];
        $data['icon'] = input('param.icon');
        $data['level'] = $level;
        $data['sort'] = input('param.sort');
           
        
        $save = AdminMenu::where('id',$all['id'])->update($data);
        if($save){
            return $this->ajaxReturn(100, '修改成功', 'default');
        }else{
            return $this->ajaxReturn(3, '修改失败', 'default');
        }
        
    }
    
    /*
     * 删除菜单
     * author xfwed
     * date 2022.03.24 15:21
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function delMenu()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'ids|ids' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        
        $data['status'] = 2;
           
        
        $save = AdminMenu::where('id','in',$all['ids'])->update($data);
        if($save){
            return $this->ajaxReturn(100, '删除成功', 'default');
        }else{
            return $this->ajaxReturn(3, '删除失败', 'default');
        }
        
    }
    
    
    
}
